package ${classInfo.packageName}.service;

import cn.hutool.core.collection.CollectionUtil;
import com.atinbo.model.Outcome;
import com.atinbo.model.PageParam;
import com.atinbo.model.Pagination;
import com.atinbo.model.SortBy;
import com.atinbo.mybatis.DynamicCondition;
import com.atinbo.mybatis.PageConvertor;
import ${classInfo.packageName}.entity.${classInfo.className};
import ${classInfo.packageName}.model.${classInfo.className}Param;
import ${classInfo.packageName}.model.${classInfo.className}Query;
import ${classInfo.packageName}.model.${classInfo.className}DTO;
import ${classInfo.packageName}.convertor.${classInfo.className}Convertor;
import ${classInfo.packageName}.mapper.${classInfo.className}Mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * ${classInfo.classComment}.
 *
 * @author ${classInfo.author}
 * @date ${.now?string('yyyy-MM-dd HH:mm:ss')}
 */
@Slf4j
@DubboService
public class ${classInfo.className}ServiceImpl implements ${classInfo.className}Service {

    @Autowired
    private ${classInfo.className}Mapper ${classInfo.className?uncap_first}Mapper;

    @Override
    public Outcome<${classInfo.className}DTO> save(final ${classInfo.className}Param ${classInfo.className?uncap_first}Param) {
        ${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className}Convertor.INSTANCE.to${classInfo.className}(${classInfo.className?uncap_first}Param);
        int count = ${classInfo.className?uncap_first}Mapper.insert(${classInfo.className?uncap_first});
        if (count > 0) {
            return Outcome.success(${classInfo.className}Convertor.INSTANCE.to${classInfo.className}DTO(${classInfo.className?uncap_first}));
        }
        return Outcome.failure("新增失败！");
    }

    @Override
    public boolean delete(final ${classInfo.className}Query query) {
        Objects.requireNonNull(query, "query不能为空");
        return ${classInfo.className?uncap_first}Mapper.delete(DynamicCondition.toWrapper(query)) > 0;
    }

    @Override
    public boolean update(final ${classInfo.className}Param ${classInfo.className?uncap_first}Param, final ${classInfo.className}Query query) {

        ${classInfo.className} entity = ${classInfo.className}Convertor.INSTANCE.to${classInfo.className}(${classInfo.className?uncap_first}Param);
        return ${classInfo.className?uncap_first}Mapper.update(entity, DynamicCondition.toWrapper(query)) > 0;
    }

    @Override
    public Outcome<${classInfo.className}DTO> findOne(final ${classInfo.className}Query queryParam) {
        List<${classInfo.className}> list = ${classInfo.className?uncap_first}Mapper.selectList(DynamicCondition.toWrapper(queryParam));
        if (CollectionUtil.isNotEmpty(list)) {
            return Outcome.success(${classInfo.className}Convertor.INSTANCE.to${classInfo.className}DTO(list.get(0)));
        }
        return Outcome.failure("未查询到！");
    }

    @Override
    public Outcome<${classInfo.className}DTO> findOne(final ${classInfo.className}Query queryParam, final SortBy sort) {
        List<${classInfo.className}> list = ${classInfo.className?uncap_first}Mapper.selectList(DynamicCondition.toWrapper(queryParam, sort));
        if (CollectionUtil.isNotEmpty(list)) {
            return Outcome.success(${classInfo.className}Convertor.INSTANCE.to${classInfo.className}DTO(list.get(0)));
        }
        return Outcome.failure("未查询到！");
    }

    @Override
    public Outcome<List<${classInfo.className}DTO>> findAll(final ${classInfo.className}Query queryParam, final SortBy sort) {
        List<${classInfo.className}> list = ${classInfo.className?uncap_first}Mapper.selectList(DynamicCondition.toWrapper(queryParam, sort));
        if (CollectionUtil.isNotEmpty(list)) {
            return Outcome.success(${classInfo.className}Convertor.INSTANCE.to${classInfo.className}DTOs(list));
        }
        return Outcome.failure("未查询到！");
    }

    @Override
    public Outcome<Pagination<${classInfo.className}DTO>> pageList(final ${classInfo.className}Query param, final PageParam pageParam) {
        Page<${classInfo.className}> page = PageConvertor.toPage(pageParam);
        IPage<${classInfo.className}> pageData = ${classInfo.className?uncap_first}Mapper.selectPage(page, DynamicCondition.toWrapper(param));
        Pagination<${classInfo.className}> pagination = PageConvertor.toPageable(pageData);
        return Outcome.success(Pagination.of(pagination, ${classInfo.className}Convertor.INSTANCE.to${classInfo.className}DTOs(pageData.getRecords())));
    }
}
